{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c9ed45d2",
   "metadata": {},
   "source": [
    "# 字符串\n",
    "\n",
    "## 字符串生成\n",
    "\n",
    "用一对单引号或者双引号来生成字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "bf338e37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world!'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello world!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "377c8b6c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world!'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'hello world!'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "703b507c",
   "metadata": {},
   "source": [
    "可以在字符串里的引号前加一个转义符号“\\”，表示字符串中的引号："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "23f64dbf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"I'm good at Python!\""
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"I\\'m good at Python!\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "106b4d6a",
   "metadata": {},
   "source": [
    "## 数学运算\n",
    "\n",
    "加法与数字乘法："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0360da68",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'helloworld'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello\" + \"world\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "46be2c83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'abcabcabc'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'abc' * 3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de59a2e9",
   "metadata": {},
   "source": [
    "长度："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "648e9cf3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len('hello world!')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a17ae33",
   "metadata": {},
   "source": [
    "## 方法\n",
    "\n",
    "`.split()`方法分割："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "abe6c275",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3', '4', '5']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"1 2 3 4    5\".split()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d8c1fb2",
   "metadata": {},
   "source": [
    "更换分隔符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7aca8d41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3', '4', '5']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"1,2,3,4,5\".split(\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "434b6d5a",
   "metadata": {},
   "source": [
    "指定最大分割次数："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ec1eee91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1', '2', '3,4,5']"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"1,2,3,4,5\".split(\",\", 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f06a9912",
   "metadata": {},
   "source": [
    "`.rsplit()`方法从后向前分割："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a2f38e4d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1,2,3', '4', '5']"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"1,2,3,4,5\".rsplit(\",\", 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd96903e",
   "metadata": {},
   "source": [
    "`.join()`方法连接："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "cf44224f",
   "metadata": {},
   "outputs": [],
   "source": [
    "nums = ['1', '2', '3', '4', '5']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "75a76070",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'1:2:3:4:5'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\":\".join(nums)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3299e9",
   "metadata": {},
   "source": [
    "`.replace()`方法替换："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "753f2204",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = 'hello world'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3b763530",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello python'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.replace(\"world\", \"python\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fae6063f",
   "metadata": {},
   "source": [
    "替换不会改变原来字符串的值："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "4169d6b2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b8029b04",
   "metadata": {},
   "source": [
    "`.find()`方法查找单词："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "0e2808ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello world\".find(\"world\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "17c4fb7d",
   "metadata": {},
   "source": [
    "`.upper()`和`.lower()`方法大小写："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "5f1e6522",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO WORLD'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"hello world\".upper()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "84e1ac8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"HELLO WORLD\".lower()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b63db6a",
   "metadata": {},
   "source": [
    "`.strip()`方法去除空白字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c0bce884",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO WORLD'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"  HELLO WORLD    \\n\\t\".strip()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "199660da",
   "metadata": {},
   "source": [
    "`.lsplit()`和`.rsplit()`方法只去除某一边的空白字符："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5aea6f2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO WORLD    '"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"  HELLO WORLD    \".lstrip()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "efedf1fc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'  HELLO WORLD'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"  HELLO WORLD    \".rstrip()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "94f74e3a",
   "metadata": {},
   "source": [
    "去除指定的字符，可以指定多个："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "00f247f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'HELLO'"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"==-==HELLO--=--\".strip(\"=-\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b4d889bd",
   "metadata": {},
   "source": [
    "## 与数字的转换\n",
    "\n",
    "数字转字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "a5377085",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'12.3'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "str(12.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "b38496e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b11111111'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bin(255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "25133570",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0o377'"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "oct(255)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "0cf14dfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0xff'"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hex(255)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a0d4570e",
   "metadata": {},
   "source": [
    "字符串转数字："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1587a7dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "255"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int('255')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "715cc9e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.5"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float('2.5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "4cd6f775",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "255"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "int('0b11111111', 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ad88e0f",
   "metadata": {},
   "source": [
    "## 格式化\n",
    "\n",
    "占位符与百分号进行格式化："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "8fc8a44e",
   "metadata": {},
   "outputs": [],
   "source": [
    "name, age = 'John', 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "dfb6c918",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'John is 10 years old.'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'%s is %d years old.' % (name, age)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff38b487",
   "metadata": {},
   "source": [
    "`.format()`方法格式化："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "6a041329",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'John is 10 years old.'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'{} is {} years old.'.format(name, age)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3902a906",
   "metadata": {},
   "source": [
    "f字符串："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "dc8951b5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'John is 10 years old.'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f'{name} is {age} years old.'"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd8163fc",
   "metadata": {},
   "source": [
    "格式化的时候指定格式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f2d5b9e7",
   "metadata": {},
   "outputs": [],
   "source": [
    "acc = 0.9131\n",
    "model_name = \"my_model\"\n",
    "epoch = 92"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b137e10c",
   "metadata": {},
   "source": [
    "冒号后面指定，`>10s`表示长度为10的字符串，`>`在前面补空格，`03d`表示长度为3、前面补0的整数，`.2f`表示保留2位小数的浮点数。`=`号会将前面的变量名一起打出来，且f字符串的变量名支持输入运算表达式。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "5bdef10e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'train   MY_MODEL, epoch=092, acc=0.91'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "f'train {model_name.upper():>10s}, {epoch=:03d}, {acc=:.2f}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f610101b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
